#! /bin/bash

IMAGE_NAME="leesah/shadowsocks-libev"
PORTNUMBER="8338"

SHADOWSOCKS="/usr/local/bin/ss-server"
HOST="-s 0.0.0.0"
PORT="-p $PORTNUMBER"
JSON=""

function print_usage {
  echo
  echo "Usage:"
  echo "  docker run $IMAGE_NAME [OPTIONS]"
  echo
  echo "OPTIONS"
  echo "  -k <password>            password of your remote server"
  echo
  echo "  [-m <encrypt_method>]    encrypt method: table, rc4, rc4-md5"
  echo "                           aes-128-cfb, aes-192-cfb, aes-256-cfb,"
  echo "                           bf-cfb, camellia-128-cfb, camellia-192-cfb,"
  echo "                           camellia-256-cfb, cast5-cfb, des-cfb, idea-cfb,"
  echo "                           rc2-cfb, seed-cfb, salsa20 and chacha20"
  echo "  [-t <timeout>]           socket timeout in seconds"
  echo "  [-c <config_file>]       config file in json"
  echo "  [-u]                     enable udprelay mode"
  echo "  [-v]                     verbose mode"
  echo
  echo "  [--fast-open]            enable TCP fast open"
  echo "  [--acl <acl_file>]       config file of ACL \(Access Control List\)"
  echo
  echo "  [-h]                     print this"
  echo
}

function print_usage_configfile {
  echo "Config file is currently not supported by this image."
  echo
  echo "See https://github.com/leesah/shadowsocks-libev/issues/1 for current progress."
  echo
}

function print_usage_host {
  echo "To specify the host on which ss-server should listen, please use"
  echo "  docker run -p $1::$PORTNUMBER ..."
  echo "or"
  echo "  docker run -p $1:<HOST-PORT>:$PORTNUMBER ..."
  echo
  echo "See manpage of docker-run for more details:"
  echo "  man docker-run"
  echo
}

function print_usage_port {
  echo "To specify the port on which ss-server should listen, please use"
  echo "  docker run -p $1:$PORTNUMBER ..."
  echo
}

OPTIONS=`getopt -o s:p:k:m:t:c:uvh --long server:,key:,password:,encrypt-method:,timeout:,acl:,server-port:,config-file:,fast-open,help -n "$IMAGE_NAME" -- "$@"`
if [ $? -ne 0 ]; then
  print_usage
  exit 1
fi

eval set -- "$OPTIONS"
while true; do
  case "$1" in
    -k|--key|--password)      PASSWORD="-k $2";         shift 2;;
    -m|--encrypt-method)      ENCRYPTION="-m $2";       shift 2;;
    -t|--timeout)             TIMEOUT="-t $2";          shift 2;;
    --acl)                    ACL="--acl $2";           shift 2;;
    --fast-open)              FAST_OPEN="--fast-open";  shift;;
    -u)                       UDP_RELAY="-u";           shift;;
    -v)                       VERBOSE="-v";             shift;;
    --)                                                 shift; break;;

    -c|--config-file)         print_usage_configfile;   exit 128;;
    -s|--server)              print_usage_host "$2";    exit 128;;
    -p|--server-port)         print_usage_port "$2";    exit 128;;
    -h|--help)                print_usage;              exit 0;;

    *)
      echo "$IMAGE_NAME: unexpected argument: $1"
      print_usage
      exit 1;;
  esac
done

if [ -z "$HOST" -o -z "$PORT" -o -z "$PASSWORD" ]; then
  echo "$IMAGE_NAME: insufficient arguments."
  print_usage
  exit 1
fi

echo "Launching Shadowsocks server..."
echo "To watch the output, run"
echo "  docker ps -ql | xargs docker logs -f"
$SHADOWSOCKS $HOST $PORT $PASSWORD $ENCRYPTION $TIMEOUT $UDP_RELAY $VERBOSE $FAST_OPEN $ACL $JSON
